/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"01754D4B-A3CE-4034-853A-66CF9B67D1F0",variableType:4}
 */
var fAnio = null;

/**
 * @type {String}
 *
 * @properties={typeid:35,uuid:"F77E7943-F30D-4307-B271-82B6C33B788D"}
 */
var fCodigo = null;

/**
 * @type {String}
 *
 * @properties={typeid:35,uuid:"CE0E9C47-63F0-40A3-9A3C-AC8CC4F2C2F3"}
 */
var fResultado = null;

/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"758BC3E0-ACBC-4B6F-A6E5-D166C295417C",variableType:4}
 */
var fEnsayo = null;

/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"F98A1F6F-DFEE-414B-9586-B2FFE415AF16",variableType:4}
 */
var fTipoMuestra = null;

/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"9DCAD959-87D5-4577-A5D8-1CD0620175E5",variableType:4}
 */
var fEdad = null;

/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"1E9DD2CB-1596-475E-9B20-3F27F91CC48F",variableType:4}
 */
var fPaciente = null;

/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"FF534620-E5B1-44A8-A3B4-7240A9BBADEF",variableType:4}
 */
var fHospital = null;

/**
 * @type {Array}
 * @properties={typeid:35,uuid:"B59A8338-2CF5-4494-9C2A-A15992332DB2",variableType:-4}
 */
var fResultados = new Array();

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"A6B4B666-C617-43E8-978A-DA1208D5D901"}
 * @AllowToRunInFind
 */
function buscar(event) {
	/** @type {String}*/
	var $where = ''
	var $args = new Array()
	
	if (fCodigo != null)
	{
		$where += ' and upper(m.codigo) like upper(?) '
		$args.push('%' + fCodigo + '%')
	}
	if (fPaciente != null)
	{
		$where += ' and m.paciente_id = ? '
		$args.push(fPaciente)
	}
	if (fTipoMuestra != null)
	{
		$where += ' and m.tipo_muestra_id = ? '
		$args.push(fTipoMuestra)
	}	
	if (fHospital != null)
	{
		$where += ' and md.hospital_id = ? '
		$args.push(fHospital)
	}
	if (fEdad != null)
	{
		$where += ' and date_part(\'year\',m.fecha) - date_part(\'year\',p.fecha_nacimiento) > ? '
		$args.push(fEdad)
	}
	if (fEnsayo != null)
	{
		$where += ' and ad.ensayo_id = ? '
		$args.push(fEnsayo)
	}
	if (fAnio != null)
	{
		$where += ' and date_part(\'year\',m.fecha) = ? '
		$args.push(fAnio)
	}
	if (fResultado != null)
	{
		var $param 	= fResultados.map(function() {return "?"}).join(",")
		$where 		+= ' and resultado IN ('+ $param+') '
		$args 		= $args.concat(fResultados)
	}
	if ($where != '')
	{
		$where = $where.substring(4,$where.length)
	
		var $query = 'SELECT ac.analisis_cab_id FROM analisis_cab as "ac" '+
						'JOIN analisis_dtl as "ad" ON ad.analisis_cab_id = ac.analisis_cab_id '+
						'JOIN muestra_destino as "md" ON md.muestra_destino_id = ac.muestra_destino_id '+
						'JOIN muestras as "m" ON m.muestra_id = md.muestra_id '+
						'JOIN pacientes as "p" ON p.paciente_id = m.paciente_id '+
						'WHERE '+ $where
		var $ds = databaseManager.getDataSetByQuery('cepario',$query,$args,-1)
		if ($ds.getMaxRowIndex() == 0)
		{
			globals.DIALOGS.showInfoDialog('Aviso','No se encontraron analisis con esas condiciones','OK')
		}
		controller.loadRecords($ds)
	}
	else
	{
		controller.loadAllRecords()
		controller.sort('fecha desc')
	}
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"DF5BBB01-1F92-4198-B7A0-FF766FE399E0"}
 */
function iraDtl(event) {
	forms.analisis_cab_dtl.controller.loadRecords(analisis_cab_id)
	globals.mostrarForm('analisis_cab_dtl')
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"60F6ED5F-43A2-4F37-8846-219C5673C019"}
 */
function anular(event) {
	fAnio			= null
	fCodigo			= null
	fEdad			= null
	fEnsayo			= null
	fHospital		= null
	fPaciente		= null
	fResultado		= null
	fResultados		= new Array()
	fTipoMuestra	= null
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"D223CC6D-1F6F-45A8-A4C4-C6285643E165"}
 */
function elegir(event) {
	globals.formEnVentana('analisis_cab_tbl_resultados','RESULTADOS','Elija los resultados')
}

/**
 * Handle changed data.
 *
 * @param {Number} oldValue old value
 * @param {Number} newValue new value
 * @param {JSEvent} event the event that triggered the action
 *
 * @returns {Boolean}
 *
 * @properties={typeid:24,uuid:"8180B7AD-5DB5-4060-9F58-36D63B9A5470"}
 */
function onDataChangeEnsayo(oldValue, newValue, event) {
	globals.gEnsayo = fEnsayo
	return true
}
